package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Override
    public IPage<RoomItemVo> pageItem(Page page, RoomQueryVo queryVo) {
        return baseMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        /*
        RoomInfo roomInfo = this.getById(id);

        //1. 查询房间关联的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,roomInfo.getApartmentId()));
        //2. 查询房间关联的图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                        .eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemType.ROOM))
                .stream().map(graphInfo -> GraphVo.builder().url(graphInfo.getUrl()).name(graphInfo.getName()).build()).collect(Collectors.toList());
        //3. 查询房间关联的属性信息列表
        */
        return baseMapper.getDetailById(id);
    }

    @Override
    public RoomDetailVo getDetailById2(Long id) {
        return baseMapper.getDetailById2(id);
    }


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper ;

    @Autowired
    private GraphInfoMapper graphInfoMapper ;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper ;

    @Autowired
    private AttrValueMapper attrValueMapper ;
    @Autowired
    private AttrKeyMapper attrKeyMapper ;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper ;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper ;

    @Autowired
    private RoomLabelMapper roomLabelMapper ;

    @Autowired
    private LabelInfoMapper labelInfoMapper ;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper ;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper ;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper ;

    @Autowired
    private LeaseTermMapper leaseTermMapper ;

    @Override
    public RoomDetailVo getDetailById3(Long id) {
        RoomInfo roomInfo = this.getById(id);
        if(roomInfo!=null) {
            RoomDetailVo roomDetailVo = new RoomDetailVo();
            BeanUtils.copyProperties(roomInfo, roomDetailVo);

            ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, roomInfo.getApartmentId()));
            roomDetailVo.setApartmentInfo(apartmentInfo);

            List<GraphInfo> graphInfoList = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
            List<GraphVo> graphVoList = graphInfoList.stream().map(graphInfo -> GraphVo.builder().url(graphInfo.getUrl()).name(graphInfo.getName()).build()).collect(Collectors.toList());
            roomDetailVo.setGraphVoList(graphVoList);

            /*
            List<RoomAttrValue> roomAttrValueList = roomAttrValueMapper.selectList(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            List<AttrValueVo> attrValueVoList = roomAttrValueList.stream()
                    .map(roomAttrValue -> {
                        AttrValue attrValue = attrValueMapper.selectById(roomAttrValue.getAttrValueId());
                        // AttrValue -> AttrValueVo
                        AttrKey attrKey = attrKeyMapper.selectById(attrValue.getAttrKeyId());
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(attrValue, attrValueVo);
                        attrValueVo.setAttrKeyName(attrKey.getName());
                        return attrValueVo;
                    }).collect(Collectors.toList());
             */
            List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoList(id);
            roomDetailVo.setAttrValueVoList(attrValueVoList);

            List<RoomFacility> roomFacilityList = roomFacilityMapper.selectList(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            List<FacilityInfo> facilityInfoList = roomFacilityList.stream().map(roomFacility -> facilityInfoMapper.selectById(roomFacility.getFacilityId())).collect(Collectors.toList());
            roomDetailVo.setFacilityInfoList(facilityInfoList);

            List<RoomLabel> roomLabelList = roomLabelMapper.selectList(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            List<LabelInfo> labelInfoList = roomLabelList.stream().map(roomLabel -> labelInfoMapper.selectById(roomLabel.getLabelId())).collect(Collectors.toList());
            roomDetailVo.setLabelInfoList(labelInfoList);

            List<RoomPaymentType> roomPaymentTypeList = roomPaymentTypeMapper.selectList(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            List<PaymentType> paymentTypeList = roomPaymentTypeList.stream().map(roomPaymentType -> paymentTypeMapper.selectById(roomPaymentType.getPaymentTypeId())).collect(Collectors.toList());
            roomDetailVo.setPaymentTypeList(paymentTypeList);

            List<RoomLeaseTerm> roomLeaseTermList = roomLeaseTermMapper.selectList(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
            List<LeaseTerm> leaseTermList = roomLeaseTermList.stream().map(roomLeaseTerm -> leaseTermMapper.selectById(roomLeaseTerm.getLeaseTermId())).collect(Collectors.toList());
            roomDetailVo.setLeaseTermList(leaseTermList);

            return roomDetailVo ;
        }else {
            return null;
        }
    }

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper ;

    @Override
    public void removeRoomById(Long id) {
        // 0. 如果当前房间有租约信息，则不能删除
        List<LeaseAgreement> leaseAgreementList = leaseAgreementMapper
                .selectList(new LambdaQueryWrapper<LeaseAgreement>()
                        .eq(LeaseAgreement::getRoomId, id)
                        .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING));

        if(leaseAgreementList!=null && leaseAgreementList.size()>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        // 1. 删除图片信息
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));
        // 2. 删除配套信息
        roomFacilityMapper.delete(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        // 3. 删除属性值信息
        roomAttrValueMapper.delete(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        // 4. 删除标签信息
        roomLabelMapper.delete(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        // 5. 删除租期信息
        roomLeaseTermMapper.delete(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        // 6. 删除支付方式信息
        roomPaymentTypeMapper.delete(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        // 7. 删除房间信息
        this.removeById(id);
    }
}




